//
//  MZBaseRootViewController.m
//  MZ_CT
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MZBaseRootViewController.h"
#import "MZDrawerViewController.h"
#import "YPHomePageVC.h"
#import "LYRadioHomeController.h"
#import "MZReadingViewController.h"
#import "MZCommunityViewController.h"
#import "MZFineViewController.h"
#import "LYSetViewController.h"
#import "UIView+Extention.h"
#import "YPJudgeNet.h"
@interface MZBaseRootViewController ()<MZDrawerViewControllerDelegate>
@property (nonatomic, assign, getter=isOpen)BOOL open;
@property (nonatomic, strong)UIViewController *homeVC;
@property (nonatomic, strong)UIViewController *radioVC;
@property (nonatomic, strong)UIViewController *readingVC;
@property (nonatomic, strong)UIViewController *communityVC;
@property (nonatomic, strong)UIViewController *fineVC;
@property (nonatomic, strong)UIViewController *settingsVC;
@property (nonatomic, strong)NSMutableArray *controllersArray;
@end

@implementation MZBaseRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"line_24px_1157236_easyicon.net"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    self.homeVC = [[UINavigationController alloc] initWithRootViewController:[[YPHomePageVC alloc] init]];
    [self addChildViewController:self.homeVC];
    [self.view addSubview:self.homeVC.view];
    self.radioVC = [[UINavigationController alloc] initWithRootViewController:[[LYRadioHomeController alloc] initWithStyle:UITableViewStylePlain]];
     self.readingVC = [[UINavigationController alloc] initWithRootViewController:[[MZReadingViewController alloc] init]];
    self.communityVC = [[UINavigationController alloc] initWithRootViewController:[[MZCommunityViewController alloc]init]];
    self.fineVC = [[UINavigationController alloc] initWithRootViewController:[[MZFineViewController alloc] initWithStyle:UITableViewStylePlain]];
    self.settingsVC = [[UINavigationController alloc] initWithRootViewController:[[LYSetViewController alloc] init]];
    self.controllersArray = @[self.homeVC, self.radioVC, self.readingVC, self.communityVC, self.fineVC, self.settingsVC].mutableCopy;
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender
{
    if (!self.isOpen) { // close
        [UIView animateWithDuration:0.3 animations:^{
            self.navigationController.view.frame = CGRectMake(300 * ([UIScreen mainScreen].bounds.size.width / 375), 0,self.view.width, self.view.hight);
        } completion:^(BOOL finished) {
            self.open = YES;
        }];
        //打开的时候
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            self.navigationController.view.frame = CGRectMake(0, 0, self.view.width, self.view.hight);
        } completion:^(BOOL finished) {
            self.open = NO;
        }];
        //关闭窗口
    }
}

- (void)getIndex:(NSInteger)index fromVC:(UIViewController *)vc str:(NSString *)str{
    if (index == 0 || index == 5) {
        [self getIndex1:index fromVC:vc str:str];
    }else{
        if ([[YPJudgeNet shareJudgeNet]isConnection] == NO ) {
            UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络异常，请检查网络" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [controller addAction:action];
            [self presentViewController:controller animated:YES completion:nil];
        }else{
            [self getIndex1:index fromVC:vc str:str];
        }
    }
}

- (void)getIndex1:(NSInteger)index fromVC:(UIViewController *)vc str:(NSString *)str
{
        for (UIViewController *childVC in self.childViewControllers) {
            [childVC.view removeFromSuperview];
            [childVC removeFromParentViewController];
        }
        UIViewController *viewController = self.controllersArray[index];
        [self addChildViewController:viewController];
        [self.view addSubview:viewController.view];
        [UIView animateWithDuration:0.3 animations:^{
            self.navigationController.view.frame = CGRectMake(0, 0, self.view.width, self.view.hight);
        } completion:^(BOOL finished) {
            self.navigationItem.title = str;
            self.open = NO;
        }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
